#pragma once

struct ToolbarText
{
	std::wstring szText;
	ui32 posX;
	ui32 length;
};

class ToolBar : public UIElement
{
	Vertex3FRHWT m_baseVertices[4];
	Texture* m_baseTexture;
	std::set<UIButton<ToolBar>*> m_buttons;
	std::set<IconButton*> m_icons;
	std::vector<ToolbarText> m_texts;
	void ButtonClicked(UIButton<ToolBar>* pButton);
	CFont m_text;
	void OnMouseMove(ui32 x, ui32 y, bool leftDown, bool rightDown);

public:
	ToolBar(ui32 y, ui32 height = 32);
	void Draw();
	void AddButton(ui32 x, ui32 w, std::string szText, Panel* pPanel);

	template<typename T>
	void AddButton(ui32 x, ui32 w, std::string szText, T* instance, typename UIButton<T>::Callback pProc, i32 texEntry = -1)
	{
		UIButton<T>* pButton = new UIButton<T>(instance, Vertex2F((float)x, m_position.y + 6), Vertex2F((float)w, 22), szText, texEntry);
		pButton->SetOnClick(pProc);
		pButton->pData = 0;
		m_buttons.insert((UIButton<ToolBar>*)pButton);
	}

	void AddIconButton(const std::string& icon, ui32 x, const std::string& szToolTip, const std::string& szMessage, std::tr1::function<void (IconButton*)> onClick)
	{
		IconButton* ibu = new IconButton(icon);
		ibu->SetSize(m_size.y - 12);
		ibu->SetPosition(x, m_position.y + 6);
		ibu->SetClickCallback(onClick);
		ibu->SetTooltip(szToolTip, szMessage);
		m_icons.insert(ibu);
	}

	void MouseActivity(bool left, Vertex2F pos, bool up);
	void SetText(ui32 index, std::string szText) {
		std::vector<wchar_t> uni(szText.length() + 1);
		MultiByteToWideChar(CP_UTF8, 0, szText.c_str(), szText.length() + 1, &uni[0], szText.length() + 1);
		SetTextW(index, &uni[0]);
	}

	void SetTextW(ui32 index, const std::wstring& szText)
	{
		if(index >= m_texts.size())
			return;
		std::wstring::const_iterator itr = szText.begin();
		std::wstring::const_iterator end = szText.end();

		ui32 glyphLen = 0;

		for( ; itr != end; ++itr)
		{
			if(m_text.GetCharWidthW((*itr)) != ui32(-1))
				glyphLen += m_text.GetCharWidthW((*itr));
		}

		m_texts[index].length = glyphLen;
		m_texts[index].szText = szText;
	}

	void SetTextPos(ui32 index, ui32 nX)
	{
		if(index >= m_texts.size())
			return;
		m_texts[index].posX = nX;
	}

	void Resize(ui32 y);

	ui32 GetTextLength(std::string szText)
	{
		std::string::iterator itr = szText.begin();
		std::string::iterator end = szText.end();

		ui32 glyphLen = 0;

		for( ; itr != end; ++itr)
		{
			if(m_text.GetCharWidthA((*itr)) != ui32(-1))
				glyphLen += m_text.GetCharWidthA((*itr));
		}

		return glyphLen;
	}

	ui32 AddTextW(ui32 x, const std::wstring& szText)
	{
		std::wstring::const_iterator itr = szText.begin();
		std::wstring::const_iterator end = szText.end();

		ui32 glyphLen = 0;

		for( ; itr != end; ++itr)
		{
			if(m_text.GetCharWidthW((*itr)) != ui32(-1))
				glyphLen += m_text.GetCharWidthW((*itr));
		}

		ToolbarText txt;
		txt.length = glyphLen;
		txt.posX = x;
		txt.szText = szText;

		m_texts.push_back(txt);
		return m_texts.size() - 1;
	}

	ui32 AddText(ui32 x, std::string szText) {
		std::vector<wchar_t> uni(szText.length() + 1);
		MultiByteToWideChar(CP_UTF8, 0, szText.c_str(), szText.length() + 1, &uni[0], szText.length() + 1);
		return AddTextW(x, &uni[0]);
	}
};